package flc.ast.fragment;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.List;

import flc.ast.R;
import flc.ast.activity.IconReplaceActivity;
import flc.ast.adapter.IconSelTabAdapter;
import flc.ast.databinding.FragmentIconSelTabBinding;
import flc.ast.utils.Content;
import stark.common.api.StkApi;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatConstant;
import stark.common.bean.StkResBean;

public class IconSelTabFragment extends BaseNoModelFragment<FragmentIconSelTabBinding> {

    private IconSelTabAdapter tabAdapter;
    private int page = 1;
    private int refreshTime = 200;
    private int oldPosition;

    @Override
    protected int onCreate() {
        return R.layout.fragment_icon_sel_tab;
    }

    @Override
    protected int getPageType() {
        return EventStatConstant.PAGE_OTHER;
    }

    public static IconSelTabFragment newInstance(String url) {
        Bundle args = new Bundle();
        args.putSerializable(Content.Url, url);
        IconSelTabFragment fragment = new IconSelTabFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    protected void initView() {
        //tab
        mDataBinding.rvTabList.setLayoutManager(new GridLayoutManager(mContext, 4));
        tabAdapter = new IconSelTabAdapter();
        mDataBinding.rvTabList.setAdapter(tabAdapter);
        tabAdapter.setOnItemClickListener(this);
        tabAdapter.notifyDataSetChanged();
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        tabAdapter.getItem(oldPosition).setSelected(false);
        tabAdapter.getItem(position).setSelected(true);
        oldPosition = position;
        IconReplaceActivity.sSelIcon = tabAdapter.getItem(position).getUrl();
        tabAdapter.notifyDataSetChanged();
    }

    @Override
    protected void initData() {
        String url = getArguments().getString(Content.Url);
        getData(url);
        mDataBinding.refreshTabItem.setRefreshHeader(new ClassicsHeader(mContext));
        mDataBinding.refreshTabItem.setRefreshFooter(new ClassicsFooter(mContext));
        mDataBinding.refreshTabItem.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                getData(url);
                mDataBinding.refreshTabItem.finishLoadMore(refreshTime);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                getData(url);
                mDataBinding.refreshTabItem.finishRefresh(refreshTime);
            }
        });

    }

    private void getData(String url) {
        StkResApi.getTagResourceList(null, url, StkApi.createParamMap(0, 28), true,
                new IReqRetCallback<List<StkResBean>>() {
                    @Override
                    public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                        if (success) {
                            if (result != null && result.size() > 0) {
                                if (page == 1) {
                                    tabAdapter.setList(result);
                                } else {
                                    tabAdapter.addData(result);
                                }
                            }
                        }
                    }
                });
    }
}
